<script>
	export default {
		data() {
			return {
				isMonitorLocationStarted: false // 用于追踪是否已经开始监听
			};
		},
		methods: {
			//开始监听实时地理位置变化
			startMonitorLocation() {
				const _this = this;
				// 如果已经开始监听，则直接返回
				if (_this.isMonitorLocationStarted) {
					return;
				}
				let startLocationUpdate = uni.startLocationUpdate();
				uni.onLocationChange(function(res) {
					_this.uploadDriverLocation2Geo();
				});
				// 标记已经开始监听
				_this.isMonitorLocationStarted = true;
			},

			//上传司机位置信息到redisGeo
			uploadDriverLocation2Geo(longitude, latitude) {
				const _this = this;
				const workStatus = uni.getStorageSync("workStatus");
				const activeStatuses = _this.Constants.ACTIVE_STATUSES;
				// 使用 includes() 方法检查 workStatus 是否在数组中
				if (activeStatuses.includes(workStatus)) {
					// 如果在数组中，执行相应逻辑
					console.log('发送给redisGeo经度：' + longitude);
					console.log('发送给redisGeo纬度：' + latitude);
					let param = {
						longitude: longitude,
						latitude: latitude,
					};
					_this.post("/driver/app/driver/uploadDriverLocation2Geo", param, (res) => {
						if (res.success) {
							console.log("---更新位置信息到GRO成功---");
						} else {
							uni.showToast({
								title: "更新位置失败",
								icon: "error"
							})
						}
					})
				}
				let orderNo = uni.getStorageSync("orderNo")
				if(workStatus == _this.Constants.WORK_STATUS.START_DRIVING){
					console.log('发送给Hbase经度：' + longitude);
					console.log('发送给Hbase纬度：' + latitude);
					let param = {
						orderNo: orderNo,
						longitude: longitude,
						latitude: latitude,
					};
					_this.post("/bigdata/app/point/save", param, (res) => {
						if (res.success) {
							console.log("---保存位置信息到Hbase成功---");
						} else {
							uni.showToast({
								title: "保存位置失败",
								icon: "error"
							})
						}
					})
				}
			}
		},
		onLaunch() {
			//保持屏幕亮着11
			wx.setKeepScreenOn({
				keepScreenOn: true
			});
			//创建上传位置到GEO的事件
			uni.$on('uploadDriverLocation2Geo', (longitude, latitude) => {
				this.uploadDriverLocation2Geo(longitude, latitude);
			});
		},
	};
</script>

<style lang="scss">
	@import 'uview-ui/index.scss';
</style>